package com.zr.bookcorner.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.gson.Gson;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.DisplayUtil;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.home.QrCodeBean;
import com.zr.bookcorner.R;
import com.zr.bookcorner.utils.DeviceUtil;
import com.zr.bookcorner.utils.ZXingUtils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnLongClick;

public class AboutActivity extends BaseNoIMActivity {
    @BindView(R.id.tv_net_type)
    TextView mTvNetType;
    @BindView(R.id.tv_version)
    TextView mTvVersion;
    @BindView(R.id.tv_device_id)
    TextView mTvDeviceId;
    @BindView(R.id.tv_location)
    TextView mTvLocation;
    @BindView(R.id.iv_qr_code)
    ImageView mIvQrCode;
    @BindView(R.id.pb)
    ProgressBar mPb;

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            initDatas();

        }
    };

    private void initDatas() {
        QrCodeBean mBean = DeviceUtil.getDeviceBean(this);
        mTvDeviceId.setText(mBean.getEquipmentCode());
        mTvLocation.setText(mBean.getLocation());
        mTvNetType.setText(mBean.getNetType() + "(" + mBean.getIp() + ")");
        mTvVersion.setText(mBean.getVersion());
        Point mPoint = DisplayUtil.getScreenMetrics(this);
        int wight = (mPoint.x / 5) * 4;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Gson mGson = new Gson();
                String content = mGson.toJson(mBean);
                Bitmap bmQR = ZXingUtils.createQRImage(content, wight, wight);
                Message msg = new Message();
                msg.obj = bmQR;
                mHandler.sendMessage(msg);
            }
        }).start();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                //杀掉以前进程
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
    }
    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mPb.setVisibility(View.GONE);
            mIvQrCode.setVisibility(View.VISIBLE);
            Bitmap bitmap = (Bitmap) msg.obj;
            mIvQrCode.setImageBitmap(bitmap);
        }
    };
    @Override
    protected int getLayoutId() {
        return R.layout.about_activity;
    }

    @Override
    protected void init() {
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(BaseConstant.BROAT_CAST_ACTION_EQUIPMENT_UPDATE);
        registerReceiver(broadcastReceiver, mIntentFilter);
        findViewById(R.id.throwException).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WeakReference<String> s=new WeakReference<>("asd");
                s=null;
                s.get();
            }
        });
        initDatas();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
    }

    @OnLongClick(R.id.tv_net_type)
    public boolean onLongClick(View view) {
        ToastView.showLong("开启adb");
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("setprop adb.tcp.port 5555\n");
            os.writeBytes("stop adbd\n");
            os.writeBytes("start adbd\n");
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
            ToastView.showLong("失败");
        }
        return  true;
    }


}
